<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class LoginFailCount{
	
	public static function add(){
		$session = Yii::app()->getSession();
		$session->open();
		$loginFailCount = $session["loginFailCount"];
		if(!isset($loginFailCount)){
			$loginFailCount = 0;
		}
		$loginFailCount = $loginFailCount+1;
		$session["loginFailCount"] = $loginFailCount;
	}
	
	public static function clean(){
		$session = Yii::app()->getSession();
		$session->open();
		$session["loginFailCount"]=0;
	}
	
	public static function current(){
		$session = Yii::app()->getSession();
		$session->open();
		$loginFailCount = $session["loginFailCount"];
		if(!isset($loginFailCount)){
			return 0;
		}
		return $loginFailCount;
	}
}
   
class LoginForm extends CFormModel
{
	public $userName;	public $password;	public $rememberMe;	public $verifyCode; 	public $canShowVC;	private $_identity;
	public $errorCount;	public $message;	public function init(){
	}

	public function rules()
	{
		$rules =  array(
		array('userName, password', 'required'),
		array('password', 'length', 'min'=>5, 'max'=>20),
		array('rememberMe', 'boolean'),
		);
		return $rules;
	}


	public function attributeLabels()
	{
		return array(
            'userName'=>'登录名',
            'password'=>'密码',
            'rememberMe'=>'记住我',
            'verifyCode'=>'验证码',
		);
	}
	public function login()
	{
		if($this->validate()){
            			if($this->_identity===null)
			{
				$this->_identity=new UserIdentity($this->userName,$this->password);
				$this->_identity->authenticate();
			}
			if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
			{
				$duration=$this->rememberMe ? 3600*24*30 : 0; 				Yii::app()->user->login($this->_identity,$duration);
				LoginFailCount::clean();
			}else{
				LoginFailCount::add();
				$this->addError("userName", "账号或密码错误");
				return false;
			}
						$user = Yii::app()->session["user"];
			Yii::app()->user->returnUrl=Yii::app()->createUrl("netdisk/index");
			return true;
		}
		return false;
	}
}
